package com.atguigu.study.controller;

import com.atguigu.study.entities.User;
import com.atguigu.study.service.UserService;
import com.atguigu.study.util.PerformanceRunnerUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @auther zzyy
 * @create 2024-12-04 17:37
 */
@RestController
@Slf4j
public class UserController
{
    @Resource
    private UserService userService;

    // http://localhost:2403/user/add
    @PostMapping(value = "/user/add")
    public int addUser(@RequestBody User user)
    {
        return userService.addUser(user);
    }



    // http://localhost:2403/user/addoptimize
    @PostMapping(value = "/user/addoptimize")
    public int addUserOptimize(@RequestBody User user)
    {
        return userService.addUserOptimize(user);
    }




















    @PostMapping(value = "/user/performance/test1")
    public void test1(@RequestBody User user) throws InterruptedException
    {
        String url = "http://localhost:2403/user/add";

        //对 url 指定的接口模拟200个并发请求，然后输出成功数量和失败的数量
        RestTemplate restTemplate = new RestTemplate();

        //调用压测工具类开始压测,模拟200个并发进行测试
        PerformanceRunnerUtil.PerformanceRunnerResult retValue = PerformanceRunnerUtil.performanceRun(
                200,
                200,
                () -> restTemplate.postForObject(url,user,Integer.class));

        System.out.println("\n压测结果如下：");
        System.out.println("==============================");
        System.out.println("请求总数 "+retValue.getRequests());
        System.out.println("并发量 "+retValue.getConcurrency());
        System.out.println("成功请求数 "+retValue.getSuccessRequests());
        System.out.println("失败请求数 "+retValue.getFailRequests());
        System.out.println("请求总耗时(ms) "+retValue.getTimeTakenForTests());
        System.out.println("每秒请求数(吞吐量) "+ retValue.getRequestsPerSecond());
        System.out.println("每个请求平均耗时(ms)"+ retValue.getTimePerRequest());
        System.out.println("最快的请求耗时(ms)"+ retValue.getFastestCostTime());
        System.out.println("最慢的请求耗时(ms)"+ retValue.getSlowestCostTime());
        System.out.println("==============================");
    }




















    @PostMapping(value = "/user/performance/test2")
    public void test2(@RequestBody User user) throws InterruptedException
    {
        String url = "http://localhost:2403/user/addoptimize";
        //http://localhost:2403/user/addoptimize

        //对 url 指定的接口模拟200个并发请求，然后输出成功数量和失败的数量
        RestTemplate restTemplate = new RestTemplate();

        //调用压测工具类开始压测,模拟200个并发进行测试
        PerformanceRunnerUtil.PerformanceRunnerResult retValue = PerformanceRunnerUtil.performanceRun(
                200,
                200,
                () -> restTemplate.postForObject(url,user,Integer.class));

        System.out.println("\n压测结果如下：");
        System.out.println("==============================");
        System.out.println("请求总数 "+retValue.getRequests());
        System.out.println("并发量 "+retValue.getConcurrency());
        System.out.println("成功请求数 "+retValue.getSuccessRequests());
        System.out.println("失败请求数 "+retValue.getFailRequests());
        System.out.println("请求总耗时(ms) "+retValue.getTimeTakenForTests());
        System.out.println("每秒请求数(吞吐量) "+ retValue.getRequestsPerSecond());
        System.out.println("每个请求平均耗时(ms)"+ retValue.getTimePerRequest());
        System.out.println("最快的请求耗时(ms)"+ retValue.getFastestCostTime());
        System.out.println("最慢的请求耗时(ms)"+ retValue.getSlowestCostTime());
        System.out.println("==============================");
    }
}
